import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;

class Request extends HashMap<String, String>
{
  private static final long serialVersionUID = 1L;
  private SocketInputStream sis;

  public Request(SocketInputStream sis)
  {
	System.out.println("\n\n\n\nRequest, Request(Socket socket)");
	this.sis = sis;

    String requestLine = readLine();
    //if( requestLine.contains("GET")) {
	    String[] requestData = requestLine.split(" ");
	    this.put("method", requestData[0]);
	    this.put("requestURI", requestData[1]);
	    this.put("version", requestData[2]);
    //}
	
	String line = "";
	try {
		while((line = sis.readLine()) != null) {
			if(line.isEmpty()) {
				break;
			}
		}
	} catch (IOException e) {
		
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }

  public String[] readLines() throws Exception
  {
	System.out.println("Request, readLines()");
    String[] lines = new String[50];
    int i = 0;
    String line = "";

    while ((line = sis.readLine()) != null)
    {
      lines[i] = line;
      i++;
    }

    return lines;
  }

  public String readLine()
  {
	System.out.println("Request, readLine()");
    try
    {
      return sis.readLine();
    } catch(SocketException e ) {
    	//System.out.println("test");
    	//System.exit(0);
    } catch (Exception e)
    {
    	e.printStackTrace();
    }
    finally
    {
    	
    }
    return null;
  }

  public String getMETHOD()
  {
	System.out.println("Request, getMETHOD()");
    return this.get("method");
  }

  public String getRequestURI()
  {
	System.out.println("Request, getRequestURI()");
    return this.get("requestURI");
  }

  public String getFilename()
  {
	System.out.println("Request, getFilename()");
	String requestURI = this.get("requestURI");
    int i = requestURI.lastIndexOf("/");
    String filename = requestURI.substring(i + 1, requestURI.length());
    return filename;
  }

  public String getFolder()
  {
	System.out.println("Request, getFolder()");
	String requestURI = this.get("requestURI");
    int lastSlash = requestURI.lastIndexOf("/");
    int firstSlash = requestURI.indexOf("/");
    String folder = requestURI.substring(firstSlash, lastSlash + 1);
    if (lastSlash != (folder.length() - 1))
    {
      folder += "/";
    }

    return folder;
  }

  public String getVERSION()
  {
	System.out.println("Request, getVERSION()");
    return this.get("version");
  }
  
  public void finalize() throws Throwable
  {
	System.out.println("Request, finalize()");
	this.clear();
	//sis.close();
	//sis = null;
  }
  
  public String toString()
  {
	System.out.println("Request, toString()");
	return getMETHOD() + " " + getRequestURI() + " " + getVERSION();
  }

 
}